package news.yp.newsproject.view.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.news.yp.baselibrary.annotation.ViewById;
import com.news.yp.baselibrary.base.BaseFragment;
import com.news.yp.baselibrary.base.BaseSkinActivity;
import com.news.yp.baselibrary.dialog.EasyDialog;
import com.news.yp.baselibrary.navigationbottom.BottomSelectListener;
import com.news.yp.baselibrary.navigationbottom.DefaultBottomTab;
import com.news.yp.baselibrary.navigationbottom.NavigationBottom;
import com.news.yp.baselibrary.navigationbottom.iterator.TabListIterator;
import com.news.yp.framelibrary.navigation.DefaultNavigationBar;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;
import news.yp.newsproject.R;
import news.yp.newsproject.view.fragment.DiscoverFragment;
import news.yp.newsproject.view.fragment.MainFragment;
import news.yp.newsproject.view.fragment.MoonFragment;
import news.yp.newsproject.view.fragment.SettingFragment;

public class MainActivity extends BaseSkinActivity {
    @ViewById(R.id.frame_layout)
    FrameLayout frameLayout;
    @ViewById(R.id.tab_bottom)
    NavigationBottom navigationBottom;
    private DefaultNavigationBar navigationBar;
    private EasyDialog easyDialog;
    private List<BaseFragment> fragmentList;
    private int lastFragmentIndex = -1;
    private TextView navTitleView;
    private TextView navRightText;
    private ImageView navImagView;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
        initPermission();
    }

    @Override
    protected void initView() {
        //初始化底部状态栏
        TabListIterator<DefaultBottomTab> listIterator = new TabListIterator<>();
        listIterator.addItem(new DefaultBottomTab.Builder(this).resIcon(R.mipmap.group1).create());
        listIterator.addItem(new DefaultBottomTab.Builder(this).resIcon(R.mipmap.group2).create());
        listIterator.addItem(new DefaultBottomTab.Builder(this).resIcon(R.mipmap.group3).create());
        listIterator.addItem(new DefaultBottomTab.Builder(this).resIcon(R.mipmap.group4).create());
        //把迭代器传过去，在LinearLayout里面尽情的发挥吧
        navigationBottom.addTabItem(listIterator);
        navigationBottom.setSelectListener(new BottomSelectListener() {
            @Override
            public void selectBottom(int i) {
                Log.e("TAG", "你点击的底部导航栏---" + i);
                if (i == lastFragmentIndex) {
                    return;
                }
                changeFragment(i);
                //修改上方的样式
                switch (i) {
                    case 0:
                        navImagView.setVisibility(View.VISIBLE);
                        navTitleView.setText("新闻");
                        navRightText.setVisibility(View.VISIBLE);
                        navRightText.setText("点击起来");
                        break;
                    case 1:
                        navImagView.setVisibility(View.INVISIBLE);
                        navTitleView.setText("阅览群书");
                        navRightText.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        navImagView.setVisibility(View.INVISIBLE);
                        navTitleView.setText("心情");
                        navRightText.setVisibility(View.INVISIBLE);
                        break;
                    case 3:
                        navImagView.setVisibility(View.INVISIBLE);
                        navTitleView.setText("设置");
                        navRightText.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        });

        //初始化Fragment
        fragmentList = new ArrayList<>(4);
        fragmentList.add(new MainFragment());
        fragmentList.add(new DiscoverFragment());
        fragmentList.add(new MoonFragment());
        fragmentList.add(new SettingFragment());

        //将Fragment添加进framelayout

        changeFragment(0);
    }

    private void changeFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (fragmentList.get(index).isAdded()) {
            //当前要展示的已经被添加过
            transaction.hide(fragmentList.get(lastFragmentIndex)).show(fragmentList.get(index));
        } else { //当前要展示的还没有被添加
            if (lastFragmentIndex == -1) {
                transaction.add(R.id.frame_layout, fragmentList.get(index));
            } else {
                transaction.hide(fragmentList.get(lastFragmentIndex)).add(R.id.frame_layout, fragmentList.get(index));
            }
        }
        lastFragmentIndex = index;
        transaction.commit();
    }

    /**
     * 初始化权限事件
     */
    private void initPermission() {
        RxPermissions permissions = new RxPermissions(this);
        permissions.requestEach(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            // Toast.makeText(MainActivity.this, "用户同意", Toast.LENGTH_SHORT).show();
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            Toast.makeText(MainActivity.this, "下次启动接着问", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this, "用户始终不同意", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    @Override
    protected void initTitle() {
        navigationBar = new
                DefaultNavigationBar.Builder(this)
                .setTitle("新闻")
                .setRightText("点击起来")
                .setRightClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        easyDialog = new EasyDialog.Builder(MainActivity.this)
                                .setContentView(R.layout.detail_comment_dialog)
                                .setOnClickListener(R.id.sure_button, new MyClickListener())
                                .wrapWidth()
                                .formBottom(true)
                                .show();
                    }
                }).builder();

        navTitleView = (TextView) navigationBar.findViewById(R.id.my_title);
        navRightText = (TextView) navigationBar.findViewById(R.id.right_text);
        navImagView = (ImageView) navigationBar.findViewById(R.id.back);
    }

    public class MyClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "确定" + view.getId(), Toast.LENGTH_SHORT).show();
            easyDialog.dismiss();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认tab
        //总是执行这句代码来调用父类去保存视图层的状态
        //super.onSaveInstanceState(outState);
    }
}

